package cn.luckdou.commons.utils;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * @class:AliyunOSSClientUtil
 * @descript:java使用阿里云OSS存储对象上传图片
 * @date:2017年3月16日 下午5:58:08
 * @author sang
 */
public class AliyunSMUtil {



    //产品名称:云通信短信API产品,开发者无需替换
    private static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    private static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
    private static final String accessKeyId = "LTAIP1U5jaW4FwfK";
    private static final String accessKeySecret = "1q0kVkzosqPaJZeij0L25z8ytS4p7g";


    public static SendSmsResponse sendSms(SendSmsRequest request) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        return sendSmsResponse;
    }

    /**
     * 发送手机验证码
     * @param phone 手机号码
     * @param code  验证码 4-6位 字符串
     * @return
     */
    public static SendSmsResponse sendPhoneCode(String phone,String code){
        String signName = "海外圈"; //短信签名
        String templateCode = "SMS_110105007"; //短信验证码模版code

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号 变量
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到  常量 定义在文件中
        request.setSignName(signName);
        //必填:短信模板-可在短信控制台中找到 常量 定义在文件中
        request.setTemplateCode(templateCode);
        //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 变量
        request.setTemplateParam("{\"code\":\""+code+"\"}");

        SendSmsResponse sendSmsResponse = null;
        try {
            sendSmsResponse = sendSms(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return sendSmsResponse;
    }

    public static void main(String[] args) throws ClientException, InterruptedException {
        //发短信
        SendSmsResponse response = sendPhoneCode("18401498568","9081");
        System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + response.getCode());
        System.out.println("Message=" + response.getMessage());
        System.out.println("RequestId=" + response.getRequestId());
        System.out.println("BizId=" + response.getBizId());
    }
}